/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: ListenerAspect
 * Author:   ZX
 * Date:     2020/7/27 19:07
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package org.example.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.example.listener.ListenedUser;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author ZX
 * @create 2020/7/27
 * @since 1.0.0
 */
@Aspect
public class ListenerAspect {
    public ListenerAspect(){
        System.out.println("ListenerAspect"+"被加载了");
    }

    @Before("execution(* org.example.controller.HelloController.*(..))")
    public void beforeController(JoinPoint jp){
        System.out.println("beforeController");
        Object[] args = jp.getArgs();
        if(args.length>0){
            System.out.println(args[0].getClass().toString());
            if(args[0].getClass().toString().equals("class org.example.listener.ListenedUser")){

                ListenedUser arg = (ListenedUser) args[0];
                HttpServletRequest request = (HttpServletRequest) args[1];
                //用户ip
                String ip = request.getRemoteAddr();
                //用户请求的url地址
                String url = request.getRequestURL().toString();
                String uri = request.getRequestURI();

                System.out.println("用户["+arg.getUsername()+"]|ip:"+ip+"|uri:"+uri+"访问开始");
            }
        }
    }

}